const { ccclass, property } = cc._decorator;

import n from "./AudioManager"
import l from "./PlayerDataSys"
import s from "./PeripheralSystemDataMgr"
import c from "./EventMgr"
import u from "./GameEventType"
import p from "./frameworkManager"
import d from "./ScrollViewCtrl"
import h from "./EngineUtil"
import f from "./BasePageCtrl"
import g from "./PageMgr"
import y from "./PeripheralSystemMgr"
import m from "./UiManage"
import _ from "./FundWithDrawPage"

@ccclass
export default class FundWithDrawPageCtrl extends f {
   public _touchControl = false;
   public _hasPeneLock = true;
   public _hasBlack = true;
   public _hasTouchLock = false;
   public _hasBlackTouch = false;
   public _showWinSize = true;

    constructor() {
        var t = this;
        t.ui = null;
        t.gold_balance = null;
        t.extract_info = null;
        t.city_info = null;
        t.click_index = null;
        return t;
    }
    public onLoad() {
        this.onUILoad();
        this.dealAnimType();
        this._touchControl = false;
        this._hasPeneLock = true;
        this._hasBlack = true;
        this._hasTouchLock = false;
        this._hasBlackTouch = false;
        this._showWinSize = true;
        super.onLoad();
        this.addButtonListen();
        this.addEvent();
    };
    public start() { };
    public _init(e?) {
        if (e) {
            var t = e.gold_balance, a = e.extract_info, o = e.city_info;
            this.gold_balance = t;
            this.extract_info = a;
            this.city_info = o;
            this.ui.cashLabel.getComponent(cc.Label).string = h.getCashBalance(this.gold_balance) + "元";
            this.initGear();
            this.addTravelPorgress();
            this.checkGuide();
            this.clickGender(this.ui.option_0);
        }
    };
    public checkGuide() {
        var e = s.getOutsideGuideStep();
        if (e ==OutSideLevelGuide.FUND_GUIDE) {
            this.scheduleOnce(function () {
                var t = "outsideLevel_guide_" + e;
                n.getInstance().playMusic(t);
                g.showPage("NoviceFundrewardPage");
            }, 1);
            g.hasShowPage("OutsideLevelGuidePage") && g.hidePage("OutsideLevelGuidePage");
            this.ui.fundNode.active = true;
            this.ui.btn_close.getComponent(cc.Button).interactable = false;
            this.ui.btn_weChatPayment.getComponent(cc.Button).interactable = false;
            this.ui.maskNode.active = true;
        } else if (e ==OutSideLevelGuide.FUND_PROVICE) {
            this.ui.fundNode.active = true;
            this.ui.btn_close.getComponent(cc.Button).interactable = false;
            this.ui.btn_weChatPayment.getComponent(cc.Button).interactable = true;
            this.ui.maskNode.active = true;
            this.ui.guide_hand.active = true;
            this.ui.goldCount.getComponent(cc.Label).string = h.getCashBalance(l.gold_balance) + "元";
            g.hasShowPage("OutsideLevelGuidePage") && g.hidePage("OutsideLevelGuidePage");
        } else {
            n.getInstance().playMusic("fund_withDraw_tip");
            this.ui.fundNode.active = true;
            this.ui.goldCount.getComponent(cc.Label).string = h.getCashBalance(l.gold_balance) + "元";
            this.ui.maskNode.active = false;
            this.ui.guide_hand.active = false;
            this.ui.btn_close.getComponent(cc.Button).interactable = true;
            this.ui.btn_weChatPayment.getComponent(cc.Button).interactable = true;
        }
    };
    public addEvent() {
        c.listen(u.UPDATE_FUND, this.updateFund, this);
        c.listen(u.FUND_AMPLIFY, this.playFundNodeAction, this);
    };
    public removeEvent() {
        c.ignore(u.UPDATE_FUND, this.updateFund, this);
        c.ignore(u.FUND_AMPLIFY, this.playFundNodeAction, this);
    };
    public updateFund() {
        var e = this;
        this.ui.btn_close.getComponent(cc.Button).interactable = false;
        var t = this.ui.fundNode.getPosition();
        t.x -= 90;
        t.y += 30;
        this.updatenNumberAnim();
        c.trigger(u.SHOWEFFECT_FLYINGRED, {
            startPos: cc.v2(0, 0),
            endPos: t,
            type: 1,
            callback: function () {
                e.ui.goldCount.getComponent(cc.Label).string = h.getCashBalance(l.gold_balance) + "元";
                e.ui.btn_weChatPayment.getComponent(cc.Button).interactable = true;
            }
        });
    };
    public playFundNodeAction() {
        var e = this;
        cc.tween(this.ui.fundNode).to(.05, {
            scale: 1.1
        }).to(.05, {
            scale: 1
        }).call(function () {
            e.ui.fundNode.scale = 1;
        }).start();
    };
    public initGear() {
        for (var e = this.ui.optionNode.children, t = 0; t < e.length; t++) {
            var a = e[t];
            this.addListen(a, this.clickGender, this);
        }
    };
    public onUILoad() {
        this.ui = this.node.addComponent(_);
    };
    public addButtonListen() {
       UiManager.addButtonListen(this.ui.btn_close, this.clickClose, this);
       UiManager.addButtonListen(this.ui.btn_weChatPayment, this.clickWechatPayment, this);
    };
    public clickGender(e) {
        var t = e.name, a = t.replace(/[^0-9]/gi, "");
        if (this.click_index != parseInt(a)) {
            this.click_index = parseInt(a);
            this.showOneSelect(t);
        }
    };
    public showOneSelect(e) {
        for (var t = this.ui.optionNode.children, a = 0; a < t.length; a++) {
            var o = t[a], i = o.getChildByName("common"), r = o.getChildByName("select");
            if (o.name == e) {
                i.active = false;
                r.active = true;
            } else {
                i.active = true;
                r.active = false;
            }
        }
    };
    public addListen(e, t, a, o) {
        void 0 === o && (o = "click");
        if (e) {
            if (!e.getComponent(cc.Button)) return;
            e.on("click", function () {
                n.getInstance().playMusic(o);
                t && t.bind(a)(e);
            }, a);
        }
    };
    public clickClose() {
        this.hide();
        s.getOutsideGuideStep() !=OutSideLevelGuide.FUND_GUIDE && n.getInstance().stopMusic("fund_withDraw_tip", false);
    };
    public addTravelPorgress() {
        this.ui.content.removeAllChildren();
        if (!this.city_info || this.city_info && 0 == this.city_info.length) p.log("城市进度为空"); else {
            this.ui.scrollView.getComponent(d).init(this.city_info);
            this.ui.scrollView.getComponent(d).scrollTotop();
        }
    };
    public clickWechatPayment() {
        if (s.getOutsideGuideStep() !=OutSideLevelGuide.FUND_PROVICE) if (null != this.click_index) {
            var e = this.extract_info[this.click_index].conditions, t = e[0].finish, a = e[1], o = e[2];
            if (parseInt(this.gold_balance) < parseInt(t)) h.showManageViewToast("旅行基金不足不能提现"); else {
                console.error("s_condition----", a, o);
                0 != a.status ? 0 != o.status ? e[3] && 0 == e[3].status ? g.showPage("FundWithDrawConditionPage", {
                    conditional: e[3],
                    type: 1,
                    gold_balance: this.gold_balance
                }) : h.showManageViewToast("旅行基金提现异常") : g.showPage("FundWithDrawConditionPage", {
                    conditional: o,
                    type: 2,
                    gold_balance: this.gold_balance
                }) : g.showPage("FundWithDrawConditionPage", {
                    conditional: a,
                    type: 1,
                    gold_balance: this.gold_balance
                });
            }
        } else h.showManageViewToast("没有选择旅行基金提现档位"); else {
            s.setOutsideStep(y.OutSideLevelGuide.CLICK_WITHDRAW);
            y.reportOutsideGuideStep(y.OutSideLevelGuide.CLICK_WITHDRAW);
            h.showManageViewToast("旅行基金不足不能提现");
            this.ui.guide_hand.active = false;
            g.showPage("OutsideLevelGuidePage");
        }
    };
    public updatenNumberAnim() {
        for (var e = 0, t = l.gold_balance - e, a = Math.abs(t), o = a < 10 ? a : 10, i = a < 10 ? 1 : Math.floor(t / 10), r = this, n = 0; n < o; n++) (function (t) {
            setTimeout( () =>{
                r.ui.cashLabel.getComponent(cc.Label).string = h.getCashBalance(e += i) + "元";
                if (t == o - 1) {
                    r.ui.cashLabel.getComponent(cc.Label).string = h.getCashBalance(l.gold_balance) + "元";
                    c.trigger(u.UPDATE_USER_GOLD);
                    r.dealGuide();
                }
            }, 60 * (t + 1));
        })(n);
    };
    public dealGuide() {
        this.ui.guide_hand.active = true;
    };
    public _onHide() {
        super._onHide.call(this);
        this.ui.scrollView.getComponent(d).recycle();
        this.ui.scrollView.getComponent(d).scrollTotop();
    };
    public onDestroy() {
        this.removeEvent();
    };
    public hide() {
        this.dealAnimType();
        super.hide.call(this);
    };
    public dealAnimType() {
        s.getOutsideGuideStep();
       OutSideLevelGuide.FUND_GUIDE, this._animType =AnimType.NONE;
    };
    // t.prefabUrl = "FundWithDrawPage";
    // t.className = "FundWithDrawPageCtrl";
    // return r([b, C("UI/pages/FundWithDrawPageCtrl")], t);
}